//
//  LFMarketViewController.m
//  LoveFruit
//
//  Created by 张令林 on 16/9/22.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "LFMarketViewController.h"
#import "NetworkingManager.h"
//#import "LFMarketModel.h"
#import "LFLeftViewModel.h"
#import "LFMarketViewCell.h"
#import "LFLeftMarketViewCell.h"
#import "LFRightMarketView.h"
#import "LFHotSellerModel.h"
#import "LFMarketDetailsController.h"
#import "FeWifiManHub.h"

@interface LFMarketViewController ()<UITableViewDataSource,UITableViewDelegate,LFMarketViewCellDelegate>
@property(nonatomic,strong)NSArray *leftArr;
@property(nonatomic,strong)NSArray *rightArr;
@property(nonatomic,weak)UITableView *leftTableView;
@property(nonatomic,weak)UITableView *rightTableView;
@property(nonatomic,strong)LFLeftViewModel *model;
@property(nonatomic,strong)NSMutableDictionary *mDict;
@property (nonatomic, assign) BOOL isRunCode;
@property(nonatomic,assign)NSIndexPath *index;
@property (strong, nonatomic) FeWifiManHub *wifiManHubLoader;
@property (strong, nonatomic) FeWifiManHub *wifiManHubPercent;
@property(strong, nonatomic) UIImageView * bigimage;
@property(nonatomic,strong)UIView *tempView;
// 头部视图

@end

@implementation LFMarketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //self.navigationController.navigationBar.alpha = 0;
    
    //调用初始化方法
  
    [self LoadData];
    [self setUpUI];
}
//-(void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    //[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"请检查网络是否连接"]];
//    
//}

-(void)LoadData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:@"5" forKey:@"call"];
    [NetworkingManager postUrlString:@"supermarket.json.php" withParam:param withSuccessBlock:^(id data) {
        NSDictionary *dict = data[@"data"];
        NSDictionary *dict1 = dict[@"products"];
        NSArray *tempArr = dict[@"categories"];
//        NSLog(@"%@--",tempArr)
        self.leftArr = [NSArray yy_modelArrayWithClass:[LFLeftViewModel class] json:tempArr];
        for (LFLeftViewModel *leftModel in self.leftArr) {
            NSArray *temp = dict1[leftModel.desc];
            self.rightArr = [NSArray yy_modelArrayWithClass:[LFHotSellerModel class] json:temp];
            [self.mDict setValue:self.rightArr forKey:leftModel.desc];
        }
            [self.leftTableView reloadData];
        [_wifiManHubLoader dismiss];
        [_bigimage removeFromSuperview];
        [_tempView removeFromSuperview];
        if (!self.isRunCode) {
            [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
            self.model = self.leftArr[0];
            [self.rightTableView reloadData];
            self.isRunCode = 1;
        }
       // NSLog(@"%@",data);
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
}
#pragma mark 初始化方法
- (void)setUpUI
{
    _bigimage = [[UIImageView alloc]initWithFrame:self.view.frame];
    _bigimage.backgroundColor = [UIColor  grayColor];
    _bigimage.alpha = 0.7;
    
    _tempView = [[UIView alloc]initWithFrame:self.view.bounds];
    _tempView.backgroundColor = [UIColor  grayColor];
    _tempView.alpha = 1;
    [self.view addSubview:_tempView];
    [self.navigationController.view addSubview:_bigimage];
    _wifiManHubLoader = [[FeWifiManHub alloc] initWithView:self.view withMode:FeWifiManHubModeOnlyLoader];
    [self.navigationController.view addSubview:_wifiManHubLoader];
    [_wifiManHubLoader show];
    //self.view.backgroundColor = [UIColor randomColor];
    UITableView *leftTableView = [[UITableView alloc] init];
    [leftTableView registerClass:[LFLeftMarketViewCell class] forCellReuseIdentifier:@"cellid"];
    leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    leftTableView.delegate = self;
    leftTableView.dataSource = self;
    [self.view addSubview:leftTableView];
    self.leftTableView = leftTableView;
    [leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.view);
        make.width.offset(100);
    }];
    UITableView *rightTableView = [[UITableView alloc] init];
    rightTableView.backgroundColor = [UIColor whiteColor];
    rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [rightTableView registerNib:[UINib nibWithNibName:@"LFMarketViewCell" bundle:nil] forCellReuseIdentifier:@"rightCell"];
    rightTableView.estimatedRowHeight = UITableViewAutomaticDimension;
    rightTableView.rowHeight = 80;
    rightTableView.delegate = self;
    rightTableView.dataSource = self;
    [self.view addSubview:rightTableView];
    self.rightTableView = rightTableView;
    [rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftTableView.mas_right);
        make.top.offset(64);
        make.bottom.offset(-49);
        make.right.equalTo(self.view);
    }];
   
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.leftTableView) {
        
        return self.leftArr.count;
    }
    else{
        NSArray *arr = self.mDict[self.model.desc];
        return arr.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.leftTableView) {
        LFLeftMarketViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid" forIndexPath:indexPath];
        LFLeftViewModel *model = self.leftArr[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.text = model.name;
        
        return cell;
    }
    else{
        LFMarketViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"rightCell" forIndexPath:indexPath];
        self.index = indexPath;
        cell1.delegate = self;
        NSArray *arr = self.mDict[self.model.desc];
        LFHotSellerModel *model = arr[indexPath.row];
        LFRightMarketView *rightView = [[LFRightMarketView alloc] init];
        cell1.selectedBackgroundView = rightView;
        cell1.rightModel = model;
        return cell1;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTableView) {
        self.index = indexPath;
        self.model = self.leftArr[indexPath.row];
        [self.rightTableView reloadData];
    }
    else{
        LFMarketDetailsController *vc = [[LFMarketDetailsController alloc] init];
        NSArray *arr = self.mDict[self.model.desc];
        LFHotSellerModel *model = arr[indexPath.row];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(NSMutableDictionary *)mDict{
    if (_mDict == nil) {
        _mDict = [NSMutableDictionary dictionary];
    }
    return _mDict;
}
-(void)getWith:(LFMarketViewCell *)cell andIndexpath:(UIImageView *)imageview andImage:(UIImage *)image{
    if (image == nil) {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"desClick" object:nil];
    }else{
        LFHotSellerModel *model = cell.rightModel;
        NSLog(@"%@",model);
        CGFloat sizeH = self.tabBarController.tabBar.frame.origin.y + 23;
        CGFloat sizeW = self.tabBarController.tabBar.frame.size.width / 5 * 3.5 ;
        CGPoint end = CGPointMake(sizeW, sizeH);
        [self addProductsAnimation:imageview dropToPoint:end isNeedNotification:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"model" object:nil userInfo:@{@"modelkey":model}];
    }
}
@end
